package com.ruoyi.web.controller.paymentcontractcost;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ProApplicationPayment;
import com.ruoyi.domain.ProjectContract;
import com.ruoyi.service.IProApplicationPaymentService;
import com.ruoyi.system.domain.ProCreateBuilding;
import com.ruoyi.system.service.IProCreateBuildingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 合同付款申请单费用Controller
 *
 * @author ruoyi
 * @date 2020-06-24
 */
@Controller
@RequestMapping("/ruoyi/paymentcontractcost")
public class PaymentContractCostController extends BaseController
{
    private String prefix = "ruoyi/paymentcontractcost";
    @Autowired
    private IProCreateBuildingService proCreateBuildingService;
    @Autowired
    private IProApplicationPaymentService proApplicationPaymentService;


    @RequiresPermissions("ruoyi:paymentcontractcost:view")
    @GetMapping()
    public String paymentcontractcost()
    {
        return prefix + "/paymentcontractcost";
    }
    /**
     * 查询项目列表
     */
    @RequiresPermissions("ruoyi:paymentcontractcost:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProCreateBuilding proCreateBuilding)
    {
        startPage();
        List<ProCreateBuilding> list = proCreateBuildingService.selectProCreateBuildingList(proCreateBuilding);
        return getDataTable(list);
    }

    /**
     * 查询对应项目下的对应的付款申请单
     */
    @RequiresPermissions("ruoyi:paymentcontractcost:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long sId) {
        startPage();
        List<ProApplicationPayment> list = proApplicationPaymentService.selectProjectContractlists(sId);
        return getDataTable(list);
    }


    /**
     * 查询对应项目下的对应的付款申请单 根据总项目id 和 付款申请单状态值为结束状态（5）的查询
     */
    @RequiresPermissions("ruoyi:paymentcontractcost:listsid")
    @PostMapping("/listsid")
    @ResponseBody
    public TableDataInfo listsid(Long sId) {
        long a = 5;
        startPage();
        List<ProApplicationPayment> list = proApplicationPaymentService.selectProjectContractlistsid(sId,a);
        return getDataTable(list);
    }

    /**
     * 导出付款申请单合同费用
     */
    @Log(title = "付款申请单数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("ruoyi:paymentcontractcost:exports")
    @PostMapping("/exports")
    @ResponseBody
    public AjaxResult exports(ProApplicationPayment proApplicationPayment)
    {
        List<ProApplicationPayment> list = proApplicationPaymentService.selectUserexport(proApplicationPayment);
        ExcelUtil<ProApplicationPayment> util = new ExcelUtil<ProApplicationPayment>(ProApplicationPayment.class);
        return util.exportExcel(list, "付款申请单数据");
    }

    /**
     * 修改付款申请单
     */
    @GetMapping("/edit/{aId}")
    public String edit(@PathVariable("aId") Long aId, ModelMap mmap) {
        ProApplicationPayment proApplicationPayment = proApplicationPaymentService.selectProApplicationPaymentById(aId);
        System.out.println(proApplicationPayment);
        mmap.put("proApplicationPayment", proApplicationPayment);
        return prefix + "/edit";
    }

    /**
     * 修改保存付款申请单
     */
    @RequiresPermissions("ruoyi:paymentcontractcost:edit")
    @Log(title = "付款申请单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProApplicationPayment proApplicationPayment) {
        ProApplicationPayment proApplicationPayment1=proApplicationPaymentService.selectProApplicationPaymentById(proApplicationPayment.getaId());

        //等于null
        if(proApplicationPayment1.getpPaid()==null&&proApplicationPayment.getpPaid().compareTo(proApplicationPayment1.getaLaborCost())<1){
            //输入的已付带入
            proApplicationPayment.getpPaid();
            //存未付  库里总金额 - 输入的已付
            proApplicationPayment.setpNotpaid(proApplicationPayment1.getaLaborCost().subtract(proApplicationPayment.getpPaid()));
            //如果已付金额不等于null
        }else if(proApplicationPayment1.getpPaid()!=null){
            //如果库已付金额 + 输入已付金额  > 库总金额  则返回错误
            if(proApplicationPayment1.getpPaid().add(proApplicationPayment.getpPaid()).compareTo(proApplicationPayment1.getaLaborCost())==1){
                return toAjax(0);
            }else {
                //sum = 库已付金额 + 输入的已付金额
                BigDecimal sum=proApplicationPayment1.getpPaid().add(proApplicationPayment.getpPaid());
                proApplicationPayment.setpPaid(sum);
                //  计算未付金额(付款总金额-已付金额)
                if(proApplicationPayment1.getpNotpaid()!=null) {
                    BigDecimal unsum = proApplicationPayment1.getaLaborCost().subtract(sum);
                    proApplicationPayment.setpNotpaid(unsum);
                }else{
                    proApplicationPayment.setpNotpaid(proApplicationPayment1.getaLaborCost().subtract(proApplicationPayment.getpPaid()));
                }

            }
        }else{
            return toAjax(0);
        }

        return toAjax(proApplicationPaymentService.updateProApplicationPayment(proApplicationPayment));
    }
}